﻿using Asmodat.Abbreviate;
using Asmodat.Extensions.Collections.Generic;
using Asmodat.Extensions.Objects;
using Asmodat.IO;
using Asmodat.Types;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Asmodat_ELM
{
    public partial class Form1 : Form
    {
        private FileDictionary<string, string> StateFile = new FileDictionary<string, string>("AppSettings.acf",0,false);

        private void SaveState_FileManager()
        {
            StateFile.Add("SearchMask", TTbxSearchMask.Text.SerializeJson(), true, true);
            StateFile.Add("SelectedFiles", SelectedFiles.SerializeJson(), true, true);
            bool[] selectOption = new bool[3] { TRBtnSelectAll.Checked, TRBtnSelectChange.Checked, TRBtnSelectSelected.Checked };
            StateFile.Add("SelectOption", selectOption.SerializeJson(), true, true);
        }

        private void SaveState_Dictionary()
        {
            StateFile.Add("Dictionary", Dictionary.SerializeJson(), true, true);
        }

        private void SaveState_Settings()
        {
            StateFile.Add("UploadTimeout", TTbxUploadTimeout.Text.SerializeJson(), true, true);
        }

        private void SaveState_SerialPort()
        {
            StateFile.Add("Baud", TCbxBaud.Text.SerializeJson(), true, true);
            StateFile.Add("Port", TCbxPort.Text.SerializeJson(), true, true);
        }


        private void SaveState()
        {
            this.SaveState_FileManager();

            this.SaveState_Dictionary();

            this.SaveState_Settings();

            this.SaveState_SerialPort();
        }

        private void LoadState()
        {

            
            TTbxSearchMask.Text = StateFile.Get("SearchMask").DeserializeJson<string>().ValueOrDefault("*.lua,*.lc");
            SelectedFiles.AddRangeDistinct(StateFile.Get("SelectedFiles").DeserializeJson<List<string>>().ValueOrDefault(new List<string>()));
            bool[] selectOption = StateFile.Get("SelectOption").DeserializeJson<bool[]>().ValueOrDefault(new bool[3] { true, false, false });
            TRBtnSelectAll.Checked = selectOption[0];
            TRBtnSelectChange.Checked = selectOption[1];
            TRBtnSelectSelected.Checked = selectOption[2];



            Dictionary = StateFile.Get("Dictionary").DeserializeJson<List<string>>().ValueOrDefault(new List<string>());
            this.UpdateDictionary();

            TTbxUploadTimeout.Text = StateFile.Get("UploadTimeout").DeserializeJson<string>().ValueOrDefault("10000");

            TCbxBaud.TrySelectItemByText(StateFile.Get("Baud").DeserializeJson<string>().ValueOrDefault("115200"));
            TCbxPort.TrySelectItemByText(StateFile.Get("Port").DeserializeJson<string>().ValueOrDefault(null));

            StateFile.Enabled = true;
        }
    }
}
